﻿using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Active.Core.State;
using System.Text;
using Active.Core;
using System.Globalization;

/// <summary>
/// Formater, 用于将字符串输出格式化的辅助类
/// </summary>
public sealed class Formater
{
    static Formater()
    {
        _numberInfo = new NumberFormatInfo();
        _numberInfo.CurrencySymbol = string.Empty;
        _numberInfo.CurrencyNegativePattern = 1;
        _numberInfo.CurrencyGroupSizes = new int[] { 3 };
    }

    /// <summary>
    /// Versions to display.
    /// </summary>
    /// <param name="sourceVersions">The source versions.</param>
    /// <returns></returns>
    public static string VersionsToDisplay(string sourceVersions)
    {
        if (sourceVersions == null)
            return string.Empty;
        return sourceVersions.Replace(Environment.NewLine, "<br />");
    }

    public static string PlayVersions(string[] playVersions)
    {
        if (playVersions == null || playVersions.Length == 0)
        {
            return "<span class='playVerson'>未定</span>";
        }
        if (playVersions.Length == 1)
        {
            return string.Format("<span class='playVerson'>{0}</span>", playVersions[0]);
        }
        string result = "";
        char index = 'A';
        foreach (string verson in playVersions)
        {
            result += string.Format("<span class='playVerson'>{0}.{1}</span>", index, verson);
            index++;
        }
        return result;
    }

    private static readonly string _STATE_IMG = "<img src='{0}' /> {1}";
    /// <summary>
    /// Gets the state text.
    /// </summary>
    /// <param name="state">The state.</param>
    /// <returns></returns>
    public static string GetStateText(object state)
    {
        if (state == null) return null;
        string stateText = state.ToString();
        if (Utils.IsWorking(stateText))
        {
            return string.Format(_STATE_IMG, VirtualPathUtility.ToAbsolute("~/pics/AuditWorking.gif"),
                Translate(stateText));
        }
        else
        {
            if (stateText == "MediaEmployeeWorking" && Utils.IsMediaManager)
            {
                return string.Format(_STATE_IMG, VirtualPathUtility.ToAbsolute("~/pics/AuditNone.gif"),
                    Translate(stateText));
            }
            return string.Format(_STATE_IMG, VirtualPathUtility.ToAbsolute("~/pics/AuditSuccess.gif"),
                Translate(stateText));
        }
    }

    /// <summary>
    /// Gets the campaign period days HTML.
    /// </summary>
    /// <param name="start">The start.</param>
    /// <param name="end">The end.</param>
    /// <returns></returns>
    public static string GetCampaignPeriodDaysHTML(DateTime start, DateTime end)
    {
        TimeSpan span = end - start;

        int totalCount = span.Days + 1;
        StringBuilder sb = new StringBuilder(totalCount);

        for (int i = 0; i < totalCount; i++)
        {
            if (start.DayOfWeek == DayOfWeek.Saturday || start.DayOfWeek == DayOfWeek.Sunday)
            {
                sb.AppendFormat("<td class=\"day sat_sun\">{0}</td>", start.Day);
            }
            else
            {
                sb.AppendFormat("<td class=\"day\">{0}</td>", start.Day);
            }
            start = start.AddDays(1);
        }
        return sb.ToString();
    }

    public static string MediaType(Active.Core.MediaType mediaType)
    {
        switch (mediaType)
        {
            case Active.Core.MediaType.TV:
                return "电视媒介";
            case Active.Core.MediaType.Publication:
                return "刊物媒介(报纸, 杂志等)";
            default:
                return "未知类型";
        }
    }

    /// <summary>
    /// Translates the specified word.
    /// </summary>
    /// <param name="word">The word.</param>
    /// <returns></returns>
    public static string Translate(string word)
    {
        return Resources.Labels.ResourceManager.GetString(word);
    }

    private static NumberFormatInfo _numberInfo;

    /// <summary>
    /// 根据订单类型，返回显示格式
    /// </summary>
    /// <param name="price"></param>
    /// <param name="category"></param>
    /// <returns></returns>
    public static string FormatPrice(decimal price, OrderType category)
    {
        if (category == OrderType.Pay || category == OrderType.Additional)
        {
            return price.ToString("c2", _numberInfo);
        }
        return Translate(category.ToString());
    }
    public static string FormatPrice(decimal price,string replaceStr)
    {
        if (price > 0)
        {

            return price.ToString("c2", _numberInfo);
        }
        return replaceStr;
    }
    public static string FormatPrice(decimal price)
    {
        if (price > 0)
        {

            return price.ToString("c2", _numberInfo);   
        }
        return "-";
    }

    public static string FormatMoney(object money)
    {
        return ((decimal)money).ToString("c2", _numberInfo);
    }
    public static string FormatTelFax(string value)
    {
        string[] arr = value.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
        Dictionary<string,string> result=new Dictionary<string,string>();
        foreach(string str in arr)
        {
            if (str.Contains("-") || str.Contains("－"))
            {
                string[] temp = str.Split(new char[] { '-', '－' });
                if(result.ContainsKey(temp[0]))
                    result[temp[0]]+="/"+temp[1];
                else
                    result.Add(temp[0], str);
            }
            else
            {
                if(result.ContainsKey("020"))
                    result["020"] += "/" + str;
                else
                    result.Add("020", "020-" + str);
            }
        }
        string res=string.Empty;
        foreach(string s in result.Values)
            res+=s+"※";
        return res.Remove(res.Length-1);
    }
}
